package com.hy.util;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletContext;

import org.springframework.stereotype.Component;

import com.hy.domain.City;
import com.hy.domain.Contact;
import com.hy.domain.Customer;
import com.hy.domain.Hotel;
import com.hy.domain.Link;
import com.hy.domain.NewType;
import com.hy.domain.News;
import com.hy.domain.Tour;

@Component
public class CommonDate {
	
	private static CommonDate instance = new CommonDate();
	
	private String dateFormat;
	
	private String[] weeks;
	
	private String hycs;
	
	private String hydt;
	
	private String hyzs;
	
	private String lybd;
	
	private CommonDate() {
		dateFormat = "yyyy年MM月dd日";
		weeks = new String[]{"星期日", "星期一", "星期二", "星期三" ,"星期四", "星期五", "星期六"};
		hycs = "会议场所";
		hydt = "会议动态";
		hyzs = "会议知识";
		lybd = "旅游宝典";
	}
	
	@SuppressWarnings("deprecation")
	public void loadDate(ServletContext context) {
		SimpleDateFormat sm = new SimpleDateFormat(dateFormat);
		Date date = new Date();
		context.setAttribute("date", sm.format(date));
		context.setAttribute("week", weeks[date.getDay()]);
		List<Contact> contacts = Contact.findContactEntries(0, 1); 
		if (contacts.size() > 0) {
			context.setAttribute("contact", contacts.get(0));
		}
		context.setAttribute("customers", Customer.findAllCustomers());
		context.setAttribute("hycs", News.getNews(NewType.findNewTypesByNameLike(hycs).getSingleResult(), 0, 11));
		context.setAttribute("zxdt", News.getNews(NewType.findNewTypesByNameLike(hydt).getSingleResult(), 0, 7));
		context.setAttribute("newTypes", NewType.findAllNewTypes());
		context.setAttribute("hyzs", News.getNews(NewType.findNewTypesByNameLike(hyzs).getSingleResult(), 0, 8));
		context.setAttribute("jdxx", Hotel.getReverseHotel(0, 10));
		context.setAttribute("zxhc", News.getNews(NewType.findNewTypesByNameLike(hycs).getSingleResult(), 0, 7));
		context.setAttribute("lybd", News.getNews(NewType.findNewTypesByNameLike(lybd).getSingleResult(), 0, 7));
		List<Tour> zxlxList = Tour.findTourEntries((int)Tour.countTours() - 10, 10);
		Collections.reverse(zxlxList);
		context.setAttribute("zxlx", zxlxList);
		context.setAttribute("links", Link.findAllLinks());
		context.setAttribute("citys", City.findAllCitys());
	}
	
	public static CommonDate getInstance() {
		return instance;
	}
	
}
